﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Read.API.application.Command.Role;
using MediatR;

namespace BasicData.Read.API.application.CommandHandler.Role
{
    public class RoleQueryCommandHandler : IRequestHandler<RoleQueryCommand, ApiResult<List<RoleModel>>>
    {
        private readonly IBaseRepository<RoleModel> roleRep;

        public RoleQueryCommandHandler(IBaseRepository<RoleModel> roleRep)
        {
            this.roleRep = roleRep;
        }
        public Task<ApiResult<List<RoleModel>>> Handle(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleModel>> res = new ApiResult<List<RoleModel>>();
            res.Code = BaseEnums.Success;
            res.Msg = "角色查询成功";
            var list = roleRep.GetAll();
            if (!string.IsNullOrEmpty(request.RoleName))
            {
                list = list.Where(o => o.RoleName.Contains(request.RoleName));
            }
            res.Data = list.ToList();

            return Task.FromResult(res);
        }
    }
}
